package com.sgcc.pda.bluetooth.ui;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.clj.fastble.data.BleDevice;
import com.sgcc.pda.bluetooth.bluetoothmanager.BleBluetoothManager;
import com.sgcc.pda.bluetooth.bluetoothmanager.ClassicBluetoothManager;
import com.sgcc.pda.mdrh.task.lib_tools_bluetool.R;

import java.util.ArrayList;
import java.util.List;

import static com.sgcc.pda.bluetooth.config.Constant.BLUE_BLE;
import static com.sgcc.pda.bluetooth.config.Constant.BLUE_CLS;

/**
 * 描 述：蓝牙设备列表适配器
 * 作 者：yangzhi
 * 时 间：2020/3/6 10:13
 * 版 本：1.0
 * 版 权： Copyright (c) remarkables-united.Co.Ltd. All rights reserved.
 */
public class DeviceAdapter extends BaseAdapter {
    //上下文
    private Context context;
    //经典蓝牙列表
    private List<BluetoothDevice> clsDevices;
    //ble列表
    private List<BleDevice> bleDevices;
    //蓝牙类型
    private int blueType;

    public DeviceAdapter(Context context) {
        this.context = context;
        clsDevices = new ArrayList<>();
        bleDevices = new ArrayList<>();
    }


    /**
     * 设置蓝牙类型
     *
     * @param type 1 经典蓝牙, 2 ble低功耗蓝牙
     */
    public void setBlueType(int type) {
        this.blueType = type;
    }

    /**
     * 添加经典蓝牙设备
     *
     * @param bluetoothDevice 经典蓝牙
     */
    public void addClsDevice(BluetoothDevice bluetoothDevice) {
        if (null != clsDevices) {
            if (!clsDevices.contains(bluetoothDevice)) {
                clsDevices.add(bluetoothDevice);
            }
        }
        this.notifyDataSetChanged();
    }

    /**
     * 添加ble蓝牙设备
     *
     * @param bleDevice ble蓝牙
     */
    public void addBleDevice(BleDevice bleDevice) {
        if (null != bleDevices) {
            if (!BleBluetoothManager.getInstance(context).isConn(bleDevice)) {
                bleDevices.add(bleDevice);
                this.notifyDataSetChanged();
            }
        }
    }

    /**
     * 添加ble蓝牙设备
     */
    public void addLastBleDevice(BleDevice bleDevice) {
        if (null != bleDevices) {
            bleDevices.add(bleDevice);
            this.notifyDataSetChanged();
        }
    }


    public void clearBleScanDevice() {
        if (null != bleDevices && !bleDevices.isEmpty()) {
            bleDevices.clear();
        }
    }

    public void clearClsScanDevice() {
        if (null != clsDevices && !clsDevices.isEmpty()) {
            clsDevices.clear();
        }
    }


    @Override
    public int getCount() {
        if (BLUE_BLE == blueType) {
            return bleDevices.size();
        } else if (BLUE_CLS == blueType) {
            return clsDevices.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (BLUE_BLE == blueType) {
            if (position >= bleDevices.size()) {
                return null;
            }
            return bleDevices.get(position);
        } else if (BLUE_CLS == blueType) {
            if (position >= clsDevices.size()) {
                return null;
            }
            return clsDevices.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = View.inflate(context, R.layout.adapter_bluetooth_device, null);
            holder = new ViewHolder();
            holder.tvName = convertView.findViewById(R.id.tv_name);
            holder.tvMac = convertView.findViewById(R.id.tv_mac);
            convertView.setTag(holder);
        }
        boolean isConnected = false;
        String name = "";
        String mac = "";
        if (BLUE_BLE == blueType) {
            //ble蓝牙
            BleDevice bleDevice = (BleDevice) getItem(position);
            if (null != bleDevice) {
                isConnected = BleBluetoothManager.getInstance(context).isConn(bleDevice);
                name = TextUtils.isEmpty(bleDevice.getName()) ? "设备名称为空" : bleDevice.getName();
                mac = bleDevice.getMac();
            }
        } else if (BLUE_CLS == blueType) {
            //经典蓝牙
            BluetoothDevice clsDevice = (BluetoothDevice) getItem(position);
            if (null != clsDevice) {
                isConnected = ClassicBluetoothManager.getInstance(context).isConnectBlue(clsDevice);
                name = TextUtils.isEmpty(clsDevice.getName()) ? "设备名称为空" : clsDevice.getName();
                mac = clsDevice.getAddress();
            }
        }
        if (isConnected) {
            holder.tvName.setTextColor(context.getResources().getColor(R.color.app_green));
            holder.tvMac.setTextColor(context.getResources().getColor(R.color.app_green));
        } else {
            holder.tvName.setTextColor(context.getResources().getColor(R.color.near_black));
            holder.tvMac.setTextColor(context.getResources().getColor(R.color.near_black));
        }
        holder.tvName.setText(name);
        holder.tvMac.setText(mac);
        return convertView;
    }

    class ViewHolder {
        TextView tvName;
        TextView tvMac;
    }

}
